﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class DistributionRelationToConfirm : BasePage
    {
        public DistributionRelationToConfirm()
        {
            this.ModuleNo = Module.分配送管理.ToString();
        }

        IBLL_Distribution iDistribution = Factory.BLL_Distribution();
        IBLL_SelectRelation iselectRelation = Factory.BLL_SelectRelation();
        IBLL_Company IBC = Factory.BLL_Company();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (CurrentAccount.GetUser().UserRole.RoleID == "05")
                {
                    this.btnQuery0.Enabled = false;
                }
                else
                {
                    HZ.DrugTrade.Model.Query.Company com = new HZ.DrugTrade.Model.Query.Company();
                    com.CompanyID = CurrentAccount.GetUser().Org.OrgID; ;
                    int outINt = 0;
                    IList<Company> lst = IBC.GetCompanyByWhere(com, 1, 1, out outINt);

                    if (lst != null && (lst[0].CompanyType == EnumCompanyType.配送企业 || lst[0].CompanyType == EnumCompanyType.生产企业直接配送 || lst[0].CompanyType == EnumCompanyType.经营企业直接配送))
                    {
                        lblNav.Text = "<ul><li class=\"active\"><a href=\"javascript:void(0);\">配送关系确认</a></li></ul>";
                        BangdinGvRelaion();
                    }
                    else
                    {
                        lblNav.Text = "<ul><li><a href=\"CompanyMaintain.aspx\">配送企业管理</a></li><li><a href=\"RelationQueryUnpack.aspx\">配送产品管理</a></li><li><a href=\"DistributionRelationToQuery.aspx\">配送关系查询</a></li><li class=\"active\"><a href=\"javascript:void(0);\">配送关系确认</a></li></ul>";
                        BangdinGvRelaion();
                    }

                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BangdinGvRelaion();
        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        private void BangdinGvRelaion()
        {
            string strWhere = GetCondition();


            strWhere += " and companyid_ps='" + CurrentAccount.GetUser().Org.OrgID + "' order by productname,goodsid,areaid,companyid_ps ";

            int totalCount = 0;
            AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE * 2;
            DataTable dt = iselectRelation.GetSelectRelationToConfirm(strWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            GvRelaion.DataSource = dt;
            GvRelaion.DataBind();
            AspNetPager1.RecordCount = totalCount;
        }

        protected string GetCondition()
        {
            string strWhere = " and ISUsing=1  ";

            #region 查询条件
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtProductName.Text.Trim())))
            {
                strWhere += " and GoodsName like '%" + HZ.Fun2.CheckString(this.txtProductName.Text.Trim()) + "%'";
            }
            //if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtPinyinCode.Text.Trim())))
            //{
            //    strWhere += " and PinyinCode like '%" + HZ.Fun2.CheckString(this.txtPinyinCode.Text.Trim()).ToUpper() + "%'";
            //}
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtOutlookc.Text.Trim()))) //规格型号
            {
                strWhere += " and Outlookc like '%" + HZ.Fun2.CheckString(this.txtOutlookc.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtMaterial.Text.Trim()))) //材质
            {
                strWhere += " and Material like '%" + HZ.Fun2.CheckString(this.txtMaterial.Text.Trim()) + "%'";
            }
            strWhere += " and Projectid ='" + ddlProjectid.SelectedValue + "'";
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtUnit.Text.Trim())))
            {
                strWhere += " and Unit like '%" + HZ.Fun2.CheckString(this.txtUnit.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtCompanyName_TB.Text.Trim())))
            {
                strWhere += " and CompanyName_TB like '%" + HZ.Fun2.CheckString(this.txtCompanyName_TB.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtpinyincode.Text.Trim()))) //拼音码
            {//'%'||UPPER('{1}')||'%'
                strWhere += " and UPPER(PinyinCode) like '%'||UPPER('" + HZ.Fun2.CheckString(txtpinyincode.Text.Trim().ToUpper()) + "')||'%'";
            }
            if (string.IsNullOrEmpty(this.DllStatusType.SelectedValue))
            {
                strWhere += "  and status<>'未提交' ";
            }
            else
            {
                strWhere += "  and status='" + DllStatusType.SelectedItem.Text + "' ";
            }
            if (string.IsNullOrEmpty(this.DllArea.SelectedValue))
            {
            }
            else
            {
                strWhere += "  and AreaName='" + DllArea.SelectedItem.Text + "' ";
            }
            #endregion

            return strWhere;
        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HZ.Web.HzLinkButton btn = e.Row.FindControl("lbtnAgree") as HZ.Web.HzLinkButton;

                btn.CommandArgument = string.Format("{3},{2},{1},{0}", this.GvRelaion.DataKeys[e.Row.RowIndex].Values["CompanyID_TB"], this.GvRelaion.DataKeys[e.Row.RowIndex].Values["AreaID"], this.GvRelaion.DataKeys[e.Row.RowIndex].Values["CompanyID_PS"], this.GvRelaion.DataKeys[e.Row.RowIndex].Values["ProcureCatalogID"]);


                HZ.Web.HzLinkButton btn2 = e.Row.FindControl("lbtnDeny") as HZ.Web.HzLinkButton;

                btn2.CommandArgument = string.Format("{3},{2},{1},{0}", this.GvRelaion.DataKeys[e.Row.RowIndex].Values["IsUsing"], this.GvRelaion.DataKeys[e.Row.RowIndex].Values["AreaID"], this.GvRelaion.DataKeys[e.Row.RowIndex].Values["CompanyID_PS"], this.GvRelaion.DataKeys[e.Row.RowIndex].Values["ProcureCatalogID"]);
                Label lblstate = e.Row.FindControl("lblstate") as Label;
                if (lblstate.Text == "已同意")
                {
                    lblstate.Text = "<font color=brown>已同意</font>";
                }
                else if (lblstate.Text == "已拒绝")
                {
                    lblstate.Text = "<font color=red>已拒绝</font>";
                }
                else if (lblstate.Text == "可配送")
                {
                    lblstate.Text = "<font color=blue>可配送</font>";
                }
            }
        }

        protected void lbtnAgree_Click(object sender, EventArgs e)
        {

            HZ.Web.HzLinkButton btn = sender as HZ.Web.HzLinkButton;
            string str = btn.CommandArgument.ToString();
            string[] array = str.Split(',');

            HZ.DrugTrade.Model.SelectRelation aAreaDistribution = new HZ.DrugTrade.Model.SelectRelation();
            aAreaDistribution.ProcureCatalogID = array[0];
            aAreaDistribution.CompanyID_PS = array[1];
            aAreaDistribution.AreaID = array[2];
            aAreaDistribution.Status = 1;
            aAreaDistribution.CompanyID_TB = array[3];
            if (iselectRelation.UpdateSelectRelation(aAreaDistribution) == 1)
            {
                //PageInfo("操作成功！", Request.RawUrl);
                HZ.Web.WebUtil.Alert("操作成功！");


            }
            else
            {
                //PageError("操作失败！", Request.RawUrl);
                //HZ.Web.MessageBox.Show("操作失败！");
                HZ.Web.WebUtil.Alert("操作失败！");
            }
            BangdinGvRelaion();
        }

        protected void lbtnDeny_Click(object sender, EventArgs e)
        {
            HZ.Web.HzLinkButton btn = sender as HZ.Web.HzLinkButton;
            string str = btn.CommandArgument.ToString();
            string[] array = str.Split(',');
            string isUsing = array[3];
            if (isUsing != false.ToString())
            {
                HZ.DrugTrade.Model.SelectRelation aAreaDistribution = new HZ.DrugTrade.Model.SelectRelation();
                aAreaDistribution.ProcureCatalogID = array[0];
                aAreaDistribution.CompanyID_PS = array[1];
                aAreaDistribution.AreaID = array[2];
                aAreaDistribution.Status = 2;
                if (iselectRelation.UpdateSelectRelation(aAreaDistribution) == 1)
                {
                    //PageInfo("操作成功！", Request.RawUrl);
                    HZ.Web.WebUtil.Alert("操作成功！");


                }
                else
                {
                    //PageError("操作失败！", Request.RawUrl);
                    HZ.Web.WebUtil.Alert("操作失败！");
                }
                BangdinGvRelaion();
            }
            else
            {
                PageError("此医疗机构正在使用中，无法删除！", Request.RawUrl);
            }
        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BangdinGvRelaion();
        }

        /// <summary>
        /// 将给定的url加上script 实现页面跳转
        /// 针对button 的 onclintclick 新窗口打开
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string OpenURLScript(string str)
        {
            int index = str.IndexOf("?");
            //给url加returnUrl,值为当前页面的Url
            if (index != -1)
            {
                str += "&" + Constants.QUERYSTRING_RETURN_URL + "=" + Page.Request.RawUrl;
            }
            else
            {
                str += "?" + Constants.QUERYSTRING_RETURN_URL + "=" + Page.Request.RawUrl;
            }
            string strScript = "if(!this.disabled){window.open('" + str + "');} return false;";
            return strScript;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected bool getCheckstatus(string str, int status)
        {
            if (status == 1)
            {
                if (str.Equals("未确定") || str.Equals("已拒绝"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (status == 2)
            {
                if (str.Equals("未确定") || str.Equals("已同意"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (status == 3)
            {
                if (str.Equals("未提交") || str.Equals("可配送"))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnAgree_Click(object sender, EventArgs e)
        {
            updateStatusBatch(1);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnDeny_Click(object sender, EventArgs e)
        {
            updateStatusBatch(2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        protected void updateStatusBatch(int status)
        {
            IList<Model.SelectRelation> lst = new List<Model.SelectRelation>();

            //遍历列表的所有行
            for (int _index = 0; _index < GvRelaion.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)GvRelaion.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked && ckb.Enabled)
                {
                    HZ.DrugTrade.Model.SelectRelation aAreaDistribution = new HZ.DrugTrade.Model.SelectRelation();
                    aAreaDistribution.ProcureCatalogID = this.GvRelaion.DataKeys[_index].Values["ProcureCatalogID"].ToString();
                    aAreaDistribution.CompanyID_PS = this.GvRelaion.DataKeys[_index].Values["CompanyID_PS"].ToString();
                    aAreaDistribution.AreaID = this.GvRelaion.DataKeys[_index].Values["AreaID"].ToString();
                    aAreaDistribution.Status = status;
                    aAreaDistribution.CompanyID_TB = this.GvRelaion.DataKeys[_index].Values["CompanyID_TB"].ToString();

                    lst.Add(aAreaDistribution);

                }
            }
            if (lst.Count > 0)
            {
                if (iselectRelation.UpdateSelectRelation(lst) == 1)
                {
                    //PageInfo("操作成功！", Request.RawUrl);
                    HZ.Web.WebUtil.Alert("操作成功！");
                }
                else
                {
                    //PageError("操作失败！", Request.RawUrl);
                    HZ.Web.WebUtil.Alert("操作失败！");
                }
                BangdinGvRelaion();
            }
            else
            {
                HZ.Web.WebUtil.Alert("至少选择一条记录！");
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {

            string strWhere = GetCondition();

            strWhere += " and companyid_ps='" + CurrentAccount.GetUser().Org.OrgID + "' order by productname ";

            int totalCount = 0;

            DataTable dt = iselectRelation.GetSelectRelationToConfirm(strWhere, 1, 0, out totalCount);

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, GvRelaion);
        }
    }
}
